package com.lijing.controller;

import com.lijing.model.*;
import com.lijing.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class FreemarkerController {

    @Autowired
    private ClubService clubService;
    @Autowired
    private UserService userService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private LeaveMsgService leaveMsgService;

    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @GetMapping("/index")
    public String index(){
        return "index";
    }

    @GetMapping("/home")
    public String home(ModelMap mop, HttpSession session){
        User user = (User) session.getAttribute("user");
        List<News> newsList;
        List<LeaveMsg> leaveMsgList;
        List<Activity> activityList;
        List<Activity> myactivityList;
        // 未加入任何社团
        if (user.getClub() == null){
            newsList = new ArrayList<News>();
            leaveMsgList = new ArrayList<LeaveMsg>();
            activityList = new ArrayList<Activity>();
            myactivityList = new ArrayList<Activity>();
        }else {
            newsList = newsService.getNewsListByClub(user.getClub().getCid());
            leaveMsgList = leaveMsgService.getLeaveMsgListByClub(user.getClub().getCid());
            activityList = activityService.selectActivityByClubAuth(user.getClub().getCid(), 0, 3);
            myactivityList = activityService.selectActivityByUser(user.getUid(), "");
            for(Activity activity:activityList){
                activity.setJoin(activityService.isJoin(activity.getAid(), user.getUid()));
                activity.setMember_count(activityService.getMemberCount(activity));
            }
            for(Activity activity:myactivityList){
                activity.setMember_count(activityService.getMemberCount(activity));
            }
        }
        mop.addAttribute("newsList", newsList);
        mop.addAttribute("leaveMsgList", leaveMsgList);
        mop.addAttribute("activityList", activityList);
        mop.addAttribute("myActivityList", myactivityList);
        return "home";
    }

    @GetMapping("/homeActivity")
    public String homeActivity(ModelMap mop, HttpSession session){
        User user = (User) session.getAttribute("user");
        if(user.getClub() == null){
            mop.addAttribute("errorMsg", "您还未加入任何社团");
            return "error_msg";
        }
        List<Activity> activityList = activityService.selectActivityByClubAuth(user.getClub().getCid(), 0, 3);
        for(Activity activity:activityList){
            activity.setJoin(activityService.isJoin(activity.getAid(), user.getUid()));
            activity.setMember_count(activityService.getMemberCount(activity));
        }
        mop.addAttribute("activityList", activityList);
        return "activity";
    }

    @GetMapping("/userInfo")
    public String userInfo(){
        return "userInfo";
    }

    @GetMapping("/clubManage")
    public String clubManage(@RequestParam("cid") Integer cid, ModelMap mop, HttpSession session){
        Club club = clubService.getClub(cid);
        User user = (User) session.getAttribute("user");
        if ("社长".equals(user.getType()) && user.getClub().getCid() == cid){
            mop.addAttribute(club);
            return "clubManage";
        }else if ("管理员".equals(user.getType())){
            return "clubManage_admin";
        }
        mop.addAttribute("errorMsg", "您没有权限");
        return "error_msg";
    }

    @GetMapping("/club")
    public String clubInfo(@RequestParam("cid")Integer cid, ModelMap mop){
        Club club = clubService.getClub(cid);
        mop.addAttribute(club);
        return "club";
    }

    @GetMapping("/addNews")
    public String addNews(HttpSession session, ModelMap mop){
        User user = (User)session.getAttribute("user");
        if ("社长".equals(user.getType()))
            return "addNews";
        else {
            mop.addAttribute("errorMsg","您没有权限！");
            return "error_msg";
        }

    }

    @GetMapping("/activityInfo")
    public String activityInfo(Integer aid, ModelMap mop, HttpSession session){
        User user = (User) session.getAttribute("user");
        Activity activity = activityService.getActivity(aid);
        activity.setJoin(activityService.isJoin(activity.getAid(), user.getUid()));
        System.out.println(activity.getAid());
        System.out.println(activity.getAname());
        mop.addAttribute("activity", activity);
        mop.addAttribute("status", new String[]{"审核中", "已通过", "空", "进行中", "已结束"});
        return "activityInfo";
    }

    @GetMapping("/article")
    public String article(Integer nid, ModelMap mop){
        News article = newsService.getNews(nid);
        mop.addAttribute("article", article);
        return "article";
    }

    @GetMapping("/register")
    public String register(){
        return "register";
    }
    //
    @GetMapping("/error_msg")
    public String error(){
        return "error_msg";
    }
}
